package com.muqi.app.qlearn.adapter;

import java.util.HashMap;
import java.util.List;

import com.bumptech.glide.Glide;
import com.muqi.app.network.http.GlideImageUtils;
import com.muqi.app.network.http.MyAsyncHttp;
import com.muqi.app.network.http.ParamsUtils;
import com.muqi.app.network.http.MyAsyncHttp.AsyncCallBackListener;
import com.muqi.app.network.http.MyAsyncHttp.RESULTCODE;
import com.muqi.app.project.contants.NetWorkApi;
import com.muqi.app.project.utils.TimeMangerUtil;
import com.muqi.app.qlearn.modles.HomeWorkBean;
import com.muqi.app.qlearn.student.R;
import android.annotation.SuppressLint;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridLayout;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class HomeWorkAdapter extends BaseAdapter {
	private Context context;
	private List<HomeWorkBean> listData;
	private OnHomeWorkListener listener = null;

	public HomeWorkAdapter(Context context, List<HomeWorkBean> listData, OnHomeWorkListener listener) {
		this.context = context;
		this.listData = listData;
		this.listener = listener;
	}

	@Override
	public int getCount() {
		return listData == null ? 10 : listData.size();
	}

	@Override
	public HomeWorkBean getItem(int position) {
		return listData.get(position);
	}
   
	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams") @Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder;
		final HomeWorkBean itemInfo = listData.get(position);
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = LayoutInflater.from(context).inflate(R.layout.item_my_homework, null);
			holder.itemAvatar = (ImageView) convertView.findViewById(R.id.homewk_logo);
			holder.itemName = (TextView) convertView.findViewById(R.id.homewk_name);
			holder.itemfollwer = (TextView) convertView.findViewById(R.id.homewk_follwer);
			holder.itemTime = (TextView) convertView.findViewById(R.id.homewk_time);
			holder.itemContent = (TextView) convertView.findViewById(R.id.homewk_content);
			holder.itemWarning = (TextView) convertView.findViewById(R.id.homewk_warning);
			holder.itemState = (TextView) convertView.findViewById(R.id.homewk_state);
			holder.itemParentState = (TextView) convertView.findViewById(R.id.homewk_parent_state);
			holder.itemCredit = (TextView) convertView.findViewById(R.id.homewk_credit);
			holder.itemPicture = (GridLayout) convertView.findViewById(R.id.homewk_pics);
			holder.itemDelete=(ImageView) convertView.findViewById(R.id.homewk_delete);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		
		if(itemInfo.subject_fileurl != null){
			GlideImageUtils.setImageLoader(context, itemInfo.subject_fileurl.fileurl_00,
					holder.itemAvatar, R.drawable.icon_homework_logo_02);
		}
		
		if(itemInfo.getConfirmState().equals("n")){
			holder.itemState.setText("未确认,");
		}else{
			holder.itemState.setText("已确认,");
			
		}
		if(itemInfo.getWithState().equals("n")){
			holder.itemParentState.setText("未完成");
		}else{
			holder.itemParentState.setText("已完成");
		}
		
		holder.itemName.setText(itemInfo.nickname);
		holder.itemfollwer.setText(itemInfo.flowercount);
		holder.itemTime.setText(TimeMangerUtil.getChinesetimeView(itemInfo.create_time));
		holder.itemContent.setText(itemInfo.content);
		if(itemInfo.warning!=null && !"".equals(itemInfo.warning)){
			holder.itemWarning.setVisibility(View.VISIBLE);
			holder.itemWarning.setText(itemInfo.warning);
		}else{
			holder.itemWarning.setVisibility(View.GONE);	
		}
		holder.itemCredit.setText(itemInfo.commentcount);
		holder.itemPicture.removeAllViews();
		if(itemInfo.classJobFiles!=null && itemInfo.classJobFiles.size()>0){
			holder.itemPicture.setVisibility(View.VISIBLE);
			for(int i=0;i<itemInfo.classJobFiles.size();i++){
				final int sonPosition = i;
				View picview = LayoutInflater.from(context).inflate(R.layout.item_picture, null);
				ImageView imageView = (ImageView) picview.findViewById(R.id.item_pic_view);
				Glide.with(context).load(itemInfo.classJobFiles.get(i).fileurlInfo.fileurl_02).dontAnimate()
						.placeholder(R.drawable.imgbtn_photo_selecter).into(imageView);
				imageView.setOnClickListener(new OnClickListener() {
					
					@Override
					public void onClick(View v) {
						listener.onClickPhoto(position, sonPosition);
					}
				});
				holder.itemPicture.addView(picview);
			}
		}else{
			holder.itemPicture.setVisibility(View.GONE);
		}
		holder.itemDelete.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				listener.onClickDelete(position);
			}
		});
		return convertView;
	}
	


	public interface OnHomeWorkListener {
		void onClickPhoto(int parentPosition, int sonPosition);
		void onClickDelete(int postion);
	}

	
	private class ViewHolder {
		ImageView itemAvatar;
		TextView itemName;
		TextView itemfollwer;
		TextView itemTime;
		TextView itemContent;
		TextView itemWarning;
		TextView itemState;
		TextView itemParentState;
		TextView itemCredit;
		GridLayout itemPicture;
		ImageView itemDelete;
	}
	
}
